package classesInterface;

import classesBancodeDados.FichaProjTerapIndividual;
import classesBancodeDados.Paciente;
import classesJpa.FichaProjTerapIndividualJpaController;
import classesJpa.exceptions.NonexistentEntityException;
import classesMiolo.DataHorario;
import classesMiolo.Formatador;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TelaFichaProjTerapeuticoIndivid extends javax.swing.JFrame {

    /**
     * Creates new form ProjTerapeuticoIndivid
     */
    private TelaFichaProjTerapeuticoIndivid() {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    private static TelaFichaProjTerapeuticoIndivid uniqueInstance;
    
    public static TelaFichaProjTerapeuticoIndivid getInstance(){
        if(uniqueInstance == null){
            uniqueInstance = new TelaFichaProjTerapeuticoIndivid();
        }
        return uniqueInstance;
    }

    public void limpaCampos(){
        this.campoAvaliacao.setText("");
        this.campoData.setText("");
        this.campoDataAdmissao.setText("");
        this.campoEquipe.setText("");
        this.campoIdade.setText("");
        this.campoMedicacao.setText("");
        this.campoNome.setText("");
        this.campoObjetivo.setText("");
        this.campoPrevisao.setText("");
        this.campoSintomatologia.setText("");
        this.campoTerapia.setText("");
    }
    
    public void preencheCampos(Paciente paciente, FichaProjTerapIndividual ficha){
        this.campoAvaliacao.setText(ficha.getAvaliacao());
        this.campoData.setText(ficha.getDataProjeto());
        this.campoDataAdmissao.setText(ficha.getDataAdmissao());
        this.campoEquipe.setText(ficha.getEquipe());
        this.campoIdade.setText(DataHorario.calculaIdade(paciente.getDataDeNascimento()) + "");
        this.campoMedicacao.setText(ficha.getMedicacao());
        this.campoNome.setText(paciente.getNome() + paciente.getSobrenome());
        this.campoObjetivo.setText(ficha.getObjetivos());
        this.campoPrevisao.setText(ficha.getPrevisaoAlta());
        this.campoSintomatologia.setText(ficha.getSintomatologia());
        this.campoTerapia.setText(ficha.getAtividadesTerapeuticasIndicadas());
    }
    
    public void turnEditable(boolean editable){
        botaoSalvar.setEnabled(editable);
        this.campoAvaliacao.setEditable(editable);
        this.campoData.setEditable(editable);
        this.campoDataAdmissao.setEditable(editable);
        this.campoEquipe.setEditable(editable);
        this.campoIdade.setEditable(false);
        this.campoMedicacao.setEditable(editable);
        this.campoNome.setEditable(false);
        this.campoObjetivo.setEditable(editable);
        this.campoPrevisao.setEditable(editable);
        this.campoSintomatologia.setEditable(editable);
        this.campoTerapia.setEditable(editable);
    }

    public FichaProjTerapIndividual getFicha() {
        return ficha;
    }

    public void setFicha(FichaProjTerapIndividual ficha) {
        this.ficha = ficha;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        campoNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoIdade = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        campoData = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        campoDataAdmissao = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoSintomatologia = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        campoMedicacao = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        campoTerapia = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        campoObjetivo = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        campoAvaliacao = new javax.swing.JTextArea();
        jLabel11 = new javax.swing.JLabel();
        campoPrevisao = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        campoEquipe = new javax.swing.JTextField();
        botaoSalvar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Projeto Terapêutico Individual");

        jLabel2.setText("Nome:");

        jLabel3.setText("Idade:");

        jLabel4.setText("Data:");

        campoData.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoDataKeyPressed(evt);
            }
        });

        jLabel5.setText("Data de Admissão:");

        campoDataAdmissao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoDataAdmissaoKeyPressed(evt);
            }
        });

        jLabel6.setText("Sintomatologia");

        campoSintomatologia.setColumns(20);
        campoSintomatologia.setRows(5);
        jScrollPane1.setViewportView(campoSintomatologia);

        jLabel7.setText("Medicação");

        campoMedicacao.setColumns(20);
        campoMedicacao.setRows(5);
        jScrollPane2.setViewportView(campoMedicacao);

        jLabel8.setText("Atividades Terapêuticas Indicadas");

        campoTerapia.setColumns(20);
        campoTerapia.setRows(5);
        jScrollPane3.setViewportView(campoTerapia);

        jLabel9.setText("Objetivos");

        campoObjetivo.setColumns(20);
        campoObjetivo.setRows(5);
        jScrollPane4.setViewportView(campoObjetivo);

        jLabel10.setText("Previsão de Alta");

        campoAvaliacao.setColumns(20);
        campoAvaliacao.setRows(5);
        jScrollPane5.setViewportView(campoAvaliacao);

        jLabel11.setText("Avaliação");

        campoPrevisao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoPrevisaoKeyPressed(evt);
            }
        });

        jLabel12.setText("Equipe:");

        botaoSalvar.setText("Salvar");
        botaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoSalvarActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoNome))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoIdade, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoDataAdmissao, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane3)
                    .addComponent(jScrollPane4)
                    .addComponent(jScrollPane5)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel11)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(campoPrevisao, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoEquipe))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaoSalvar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoIdade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(campoData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(campoDataAdmissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(campoPrevisao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(campoEquipe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoSalvar)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoSalvarActionPerformed
        FichaProjTerapIndividualJpaController jpa = new FichaProjTerapIndividualJpaController();
        ficha.setAtividadesTerapeuticasIndicadas(campoTerapia.getText());
        ficha.setAvaliacao(campoAvaliacao.getText());
        ficha.setDataAdmissao(campoDataAdmissao.getText());
        ficha.setDataProjeto(campoData.getText());
        ficha.setEquipe(campoEquipe.getText());
        ficha.setMedicacao(campoMedicacao.getText());
        ficha.setObjetivos(campoObjetivo.getText());
        ficha.setPrevisaoAlta(campoPrevisao.getText());
        ficha.setSintomatologia(campoSintomatologia.getText());
        try {
            jpa.edit(ficha);
        } catch (NonexistentEntityException ex) {
            JOptionPane.showMessageDialog(null, "Ficha não encontrada.");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Erro na comunicação com o Banco de Dados.");
        }
        this.setVisible(false);
    }//GEN-LAST:event_botaoSalvarActionPerformed

    private void campoDataKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDataKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoData.setText(Formatador.formataData(campoData.getText()));
        }
    }//GEN-LAST:event_campoDataKeyPressed

    private void campoDataAdmissaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoDataAdmissaoKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoDataAdmissao.setText(Formatador.formataData(campoDataAdmissao.getText()));
        }
    }//GEN-LAST:event_campoDataAdmissaoKeyPressed

    private void campoPrevisaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoPrevisaoKeyPressed
        if(evt.getKeyChar() != '\b'){
            campoPrevisao.setText(Formatador.formataData(campoPrevisao.getText()));
        }
    }//GEN-LAST:event_campoPrevisaoKeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        limpaCampos();
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        limpaCampos();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaFichaProjTerapeuticoIndivid.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaFichaProjTerapeuticoIndivid.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaFichaProjTerapeuticoIndivid.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaFichaProjTerapeuticoIndivid.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaFichaProjTerapeuticoIndivid().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoSalvar;
    private javax.swing.JTextArea campoAvaliacao;
    private javax.swing.JTextField campoData;
    private javax.swing.JTextField campoDataAdmissao;
    private javax.swing.JTextField campoEquipe;
    private javax.swing.JTextField campoIdade;
    private javax.swing.JTextArea campoMedicacao;
    private javax.swing.JTextField campoNome;
    private javax.swing.JTextArea campoObjetivo;
    private javax.swing.JTextField campoPrevisao;
    private javax.swing.JTextArea campoSintomatologia;
    private javax.swing.JTextArea campoTerapia;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    // End of variables declaration//GEN-END:variables
    FichaProjTerapIndividual ficha;
}
